package example;

import java.net.*;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.io.*;
import java.util.*;

import queryTree.AbstractNode;
import queryTree.JoinNode;
import queryTree.Main;

import com.sun.org.apache.bcel.internal.generic.LSTORE;

import costEstimation.Optimizer;
import costEstimation.Optimizer2;

public class ProcessThread extends Thread {
    
    public SocketList[] participant = new SocketList[BFFS];    
    private static final int timeout_length =20000;
    public boolean isParticipant = false;
    public  DBConnection dbConn = null;
    public boolean isFinish = false;
    private Socket socket = null;
    public static int BFFS = 50;
    public SimpleProtocol kpp;
    public String tableName = ""; //tableName correspondent with its server
    public String txnTable = "";//use for participant
    public int currentCmd = 0;
    public String[] cmdBuffer;
    public int transactionID;
    int iParticipant = 0;
    int iSocket = 0;
    protected int m_rate = 5000;
    
    //Query processing
    ArrayList<Tuples> localResult = new ArrayList<Tuples>();
    ArrayList<Tuples> foreignResult = new ArrayList<Tuples>();
    boolean controlSite = false;
    boolean interSite = false;
    boolean remoteSite = false;
    int queryType;
    boolean settle = false;
    
    //this thread is data thread
    //run at data server
    //get request from master server
    //execute query
    //return result to master result
    //and done
    public ProcessThread(Socket socket, SimpleProtocol kpp, String tableName, String locktable) throws RemoteException, Exception {
		super("ProcessThread");
		this.socket = socket;
		this.kpp = kpp;
		this.tableName = tableName.toUpperCase();
		cmdBuffer = new String[BFFS];
		dbConn = new DBConnection();
		participant = new SocketList[BFFS];
		iParticipant = 0;
		System.out.println("tablename = "+this.tableName);
//		this.transactionID = dbConn.getCurrentTransactionNumber(this.tableName);
//		dbConn.updateCurrentTransactionNumber(this.tableName);
    }
   
    //input: tableName
    //output: Host IP
    @SuppressWarnings("deprecation")
	public void run(){
	txnTable = this.tableName;
	try {
	    
	    ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
	    ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
	    String inputLine ="";
	    MixProtocol mp = new MixProtocol();
	    util u = new util();
	    MixProtocol jQuery = new MixProtocol();
	    
	    MixProtocol client = new MixProtocol();
    	
//	   while (!isFinish && (mp = (MixProtocol)in.readObject()) != null) {
//			queryType = mp.type;
////			System.out.println("this.tableName "+this.tableName+" mp.interSite "+mp.interSite);
//			String localQuery = mp.joinquery;
//			System.out.println("intersite localQuery = "+localQuery);
//	
//			MiniTask mini  = new MiniTask(localQuery);
//			//executed and save result in mini.localResult
//	    	mini.run();
//	    	client.result = mini.localResult;
////	    	out.writeObject(client);
//	   }    
	}
	 catch (IOException e) {
	    e.printStackTrace();
	    System.out.println("Server IO exception "+ e.getMessage());
	    
	}
	 catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
    }
}
